<?php

namespace App\Http\Controllers\Web\V1;

use App\Http\Controllers\Controller;
use App\Models\StudentResult;
use App\Models\User;
use App\Transformers\Web\RankTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class RankController extends Controller
{
    /**
     * todo 有bug
     * @param Request $request
     * @param $type //type 1考试排行 2练习排行 3闯关排行 4积分排行
     * @return array|void
     */
    public function index(Request $request, $type)
    {

        $date = $request->input('date', 'day');

        //现在时间
        $now = Carbon::now();
        //当前月第一天
        $firstDay = $now->startOfMonth()->toDateTimeString();
        //当前最后一天
        $lastDay = $now->endOfMonth()->toDateTimeString();

        if ($type == 4) {
            if ($date == 'day') {//积分日榜 day
                $day_integral = User::query()
                    ->select('id', 'avatar', 'username', 'day_integral')
                    ->orderBy('day_integral', 'desc')
                    ->limit(3)
                    ->get();

                return $this->response->array(['day' => $day_integral]);

            } elseif ($date == 'week') {//积分周榜 day
                $week_integral = User::query()
                    ->select('id', 'avatar', 'username', 'week_integral')
                    ->orderBy('week_integral', 'desc')
                    ->limit(3)
                    ->get();

                return $this->response->array(['week' => $week_integral]);

            } elseif ($date == 'month') {//积分月榜 month
                $month_integral = User::query()
                    ->select('id', 'avatar', 'username', 'month_integral')
                    ->orderBy('month_integral', 'desc')
                    ->limit(3)
                    ->get();

                return $this->response->array(['month' => $month_integral]);

            } else {
                return $this->response->errorBadRequest('参数错误');
            }
        } elseif ($type == 1 || $type == 2 || $type == 3) {

            if ($date == 'day') {
                $day_result = StudentResult::query()
                    ->whereBetween('end_time', [Carbon::today(), Carbon::tomorrow()])
                    ->where('type', $type)
                    ->select('fraction', 'user_id', 'username', 'subject_id', 'timer')
                    ->with('subject:id,name')
                    ->orderBy('fraction', 'desc')
                    ->limit(3)
                    ->get();

                return $this->response->collection($day_result,new RankTransformer());

            } elseif ($date == 'week') {
                $week_result = StudentResult::query()
                    ->whereBetween('end_time', [Carbon::parse('last monday'), Carbon::parse('sunday')])
                    ->where('type', $type)
                    ->select('fraction', 'user_id', 'username', 'subject_id', 'timer')
                    ->with('subject:id,name')
                    ->orderBy('fraction', 'desc')
                    ->limit(3)
                    ->get();

                return $this->response->collection($week_result,new RankTransformer());
            } elseif ($date == 'month') {
                $month_result = StudentResult::query()
                    ->whereBetween('end_time', [$firstDay, $lastDay])
                    ->where('type', $type)
                    ->select('fraction', 'user_id', 'username', 'subject_id', 'timer')
                    ->with('subject:id,name')
                    ->orderBy('fraction', 'desc')
                    ->limit(3)
                    ->get();

                return $this->response->collection($month_result,new RankTransformer());
            } else {
                return $this->response->errorBadRequest('参数错误');
            }
        } else {
            return $this->response->errorBadRequest('参数错误');
        }


    }
}
